import { AssetManager, Prefab, assetManager, instantiate,Node, Script, director } from "cc";
import { Logger } from "./API";

export class GameSDK {
    private static _ins:GameSDK
    public static getInstance():GameSDK{
        if(!GameSDK._ins)GameSDK._ins=new GameSDK();
        return GameSDK._ins;
    }
    /**
     * 初始化游戏SDK
     * @param skdPath 
     * @param version 
     * @param path  
     * @param onComplete  
     */
    public initGameSDK(skdPath:string,ver:string,path:string,onComplete?:()=>void){
        assetManager.loadBundle(skdPath,{version:ver},(err: Error | null, bundle: AssetManager.Bundle)=>{
            if(!err){
                bundle.loadDir("./",(err: Error | null, data: any[])=>{
                    if(!err){
                        const sdkPrefab:Prefab = bundle.get(path)
                        if(sdkPrefab){
                            const sdk:Node = instantiate(sdkPrefab)
                            director.getScene().addChild(sdk)
                            Logger.Warn("GameSDK init completed")
                        }
                    }else{
                        console.error("GameSDK init error")
                    }
                    onComplete && onComplete();
                })
            }else{
                console.error("GameSDK loading error")
                onComplete && onComplete(); 
            }
        })
    }
    
}


